package com.aat.dxfy.safe;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class SafeFileUtil {

	public static void main(String[] args) {
		//a2b("E:\\Desktop\\wx\\read\\test.ppt");
		//b2a("E:\\Desktop\\wx\\read\\test.dat");
	}

	public static void a2b(String filepath) {
		try{
			File file=new File(filepath);
			String fname=file.getName();
			String fpath=file.getParent();
			
			byte[] fnamebs=fname.getBytes();
			byte[] namebs=new byte[100];
			System.arraycopy(fnamebs, 0, namebs, 0, fnamebs.length);
			
			InputStream in=new FileInputStream(file);
			OutputStream out=new FileOutputStream(fpath+File.separator+System.currentTimeMillis()+".dat");
			out.write(namebs);
			byte[] bs=new byte[1024];
			int i=0;
			while((i=in.read(bs))!=-1){
				out.write(bs,0,i);
			}
			out.flush();
			out.close();
			in.close();
			
		}catch(Exception e){
			
		}
	}

	public static void b2a(String filepath) {
		try{
			File file=new File(filepath);
			InputStream in=new FileInputStream(file);
			byte[] namebs=new byte[100];
			int i = in.read(namebs);
			String name=new String(namebs);
			byte[] bs=new byte[1024];
			String f2s=file.getParent()+File.separator+name;
			f2s=f2s.trim();
			File f2=new File(f2s);
			if(!f2.exists()){
				f2.createNewFile();
			}
			OutputStream out=new FileOutputStream(f2);
			while((i=in.read(bs))!=-1){
				out.write(bs, 0, i);
			}
			out.flush();
			in.close();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
